package com.sqly.doc.menu.grjbxx;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cazaea.sweetalert.SweetAlertDialog;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.adapter.CommonAdapter;
import com.sqly.doc.adapter.ViewHolder;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.pojo.Team;
import com.sqly.doc.pojo.Yldw;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.SweetAlertDialogUtil;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.StringUtils;
import com.sqly.doc.view.XListView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public class TreeXzqhActivity extends Activity {

    LinearLayout back;
    TextView title,uname;
    private ListView mListView;
    private NodeTreeAdapter mAdapter;
    private LinkedList<Node> mLinkedList = new LinkedList<>();
    Context con = TreeXzqhActivity.this;
    List<Node> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_xzqh);
        back = (LinearLayout)findViewById(R.id.back);
        title = (TextView)findViewById(R.id.title);
        uname = (TextView)findViewById(R.id.uname);
        title.setText("选择行政区划");
        uname.setText("确定");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView = (ListView)findViewById(R.id.id_tree);
        mAdapter = new NodeTreeAdapter(this,mListView,mLinkedList);
        mListView.setAdapter(mAdapter);
        initData();

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Node node = mLinkedList.get(position);
                if(node.isLeaf() && "4".equals(node.get_dwjb())){
                    new SweetAlertDialog(con, SweetAlertDialog.WARNING_TYPE)
                            .setTitleText("你确认选择该行政区划吗")//标题
                            .setContentText("您已选择“"+node.get_label()+"”，确认请点击“确定”，重新选择请点击“返回”")//提示内容
                            .setCancelText("确定")//取消按钮文本
                            .setConfirmText("返回")//确认按钮文本
                            .showCancelButton(true)
                            .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sDialog) {
                                    sDialog.dismiss();
                                }
                            })
                            .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sDialog) {
                                    Intent intent = new Intent(con, getIntent().getStringExtra("cname").getClass());
                                    intent.putExtra("xzqhname", node.get_label());
                                    intent.putExtra("xzqhid", node.get_id()+"");
                                    setResult(1, intent);
                                    sDialog.dismiss();
                                    finish();
                                }
                            })
                            .show();
                }else {
                    mAdapter.expandOrCollapse(position);
                }
            }
        });
    }

    private void initData(){
        //addOne(data);
        {
            RequestParams ps = new RequestParams();
            ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
            ps.add("type", "2");
            ps.add("page", 1000 + "");

            UserClient.get(HttpUrl.getCunJiOrgTree, ps, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {
                    super.onStart();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                    MyToastUtil.ShowToast(con, "请求失败");
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                    String content = new String(response);
                    RetMsg ret = JSON.parseObject(content, RetMsg.class);
                    if (ret.getCode().equals("200")) {
                        List<Yldw> ll = JSON.parseArray(ret.getList(), Yldw.class);
                        for (int i = 0; i < ll.size(); i++) {
                            Yldw yldw = ll.get(i);
                            data.add(new Dept(yldw.getNid(), StringUtils.isEmpty(yldw.getPid()) ? "0" : yldw.getPid(), yldw.getName(),yldw.getXzqh(), yldw.getType()));
                        }
                        mLinkedList.addAll(NodeHelper.sortNodes(data));
                        mAdapter.notifyDataSetChanged();
                    } else {
                        MyToastUtil.ShowToast(con, "获取错误");
                    }

                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
